<?php defined('SYSPATH') or die('No direct script access.');

abstract class Page extends Controller_Template {

    public $main_config;
    public $title;
    public $keywords;
    public $description;
    public $upload_thumbs;
    public $upload_original;

    public function before()
    {
        $this->main_config = Kohana::$config->load('main');
        $this->upload_thumbs = Upload::$default_directory.'/'.$this->main_config->get('upload_thumbnails_dir');
        $this->upload_original = Upload::$default_directory.'/'.$this->main_config->get('upload_original_dir');

        parent::before();

        $this->add_styles(array(
            'style',
        ));
        $this->add_scripts(array(
            'jquery.min',
            'jquery-ui-1.8.2.custom.min',
            'main',
        ));
    }

    public function after()
    {
        View::set_global(array(
            'baseurl'           => Kohana::$base_url,
            'upload_thumbs'     => $this->upload_thumbs,
            'upload_original'   => $this->upload_original,
            'title'             => (! isset($this->title)) ? $this->main_config->get('title') : $this->title,
            'keywords'          => (! isset($this->keywords)) ? $this->main_config->get('keywords') : $this->keywords,
            'description'       => (! isset($this->description)) ? $this->main_config->get('description') : $this->description,
        ));

        parent::after();
    }

    public function add_scripts(array $scripts)
    {
        if (count($scripts) > 0)
        {
            if ( ! isset($this->template->scripts))
            {
                $this->template->scripts = $scripts;
            }
            else
            {
                foreach ($scripts as $script)
                {
                    $this->template->scripts[] = $script;
                }
            }
        }
    }

    public function add_styles(array $styles)
    {
        if (count($styles) > 0)
        {
            if ( ! isset($this->template->styles))
            {
                $this->template->styles = $styles;
            }
            else
            {
                foreach ($styles as $style)
                {
                    $this->template->styles[] = $style;
                }
            }
        }
    }
}